var isMapShow = false;
var totalSum = 0;

$(function() {
    $('.map_hotel_container').hide();
    $('.rooms_hotel_date').hide();

    $('.main_images img').each(function(i, v) {
        if (i > 0) {
            $(v).hide();
        } else {
            $(v).attr({'selected': 'selected'});
        }
    });

    //*********** CARUSEL ***************//
    $('.arrows_left_image').on('click', function() {
        changeImage('left');
    });

    $('.arrows_rigth_image').on('click', function() {
        changeImage('right');
    });

    //*** DatePicker
    var nightOption = $('#date_rooms_hotel_night');

    for (var i = 1; i <= 45; i++) {
        if (i === 1) {
            nightOption.append('<option value="' + i + '" selected="selected">' + i + '</option>');
        } else {
            nightOption.append('<option value="' + i + '">' + i + '</option>');
        }
    }

    $('#date_rooms_hotel_date_in').datepicker({
        minDate: new Date(getCurrentDate()),
        dateFormat: 'yy-mm-dd',
        onClose: function(selectedDate) {
            var night = $('#date_rooms_hotel_night').val();
            $('#date_rooms_hotel_date_out').val(getOutDate(night, selectedDate));
            $("#date_rooms_hotel_date_out").datepicker("option", "minDate", selectedDate);
        }
    });

    $('.rooms_hotel_date_in img').click(function() {
        $('#date_rooms_hotel_date_in').datepicker("show");
    });

    $('#date_rooms_hotel_date_out').datepicker({
        dateFormat: 'yy-mm-dd',
        onClose: function(selectedDate) {
            $('#date_rooms_hotel_night').val(createNight($('#date_rooms_hotel_date_in').val(), selectedDate));
            $("#date_rooms_hotel_date_in").datepicker("option", "maxDate", selectedDate);
        }
    });

    $('.rooms_hotel_date_out img').click(function() {
        $('#date_rooms_hotel_date_out').datepicker("show");
    });

    $('#date_rooms_hotel_night').on('click', function() {
        if ($('#date_rooms_hotel_date_in').val() !== "") {
            $('#date_rooms_hotel_date_out').val(getOutDate($('#date_rooms_hotel_night').val(), $('#date_rooms_hotel_date_in').val()));
        }
    });

    //********** MAPS

    $('.show_map').on('click', function() {
        if ($('.show_map').text() === 'Show') {
            $('.map_hotel_container').slideDown();
            $('.show_map').text('Hide');
            if (!isMapShow) {
                isMapShow = true;
                createGoogleMap();
            }
        } else {
            $('.map_hotel_container').slideUp();
            $('.show_map').text('Show');
        }
    });

    $('.change_date label').on('click', function() {
        if ($(this).text() === 'Change date') {
            $('.rooms_hotel_date').slideDown('fast');
            $(this).text('Close');
        } else {
            $('.rooms_hotel_date').slideUp('fast');
            $(this).text('Change date');
        }
    });

    $('.hotel_image_column_container').hover(function() {
        $('.arrows_image_container').show();
    }, function() {
        $('.arrows_image_container').hide();
    });

    $('.rooms_hotel_date_update').on('click', function() {
        var hotel_id = $('.header_center_block').attr('value');
        var civic = $('.header_center_block').attr('civic');
        var date = $('#date_rooms_hotel_date_in').val();
        var night = $('#date_rooms_hotel_night').val();

        window.location.assign(urlBookHotel + '?hotel_id=' + hotel_id + '&date=' + date + '&night=' + night + '&civic=' + civic);
    });

    $('.room_button_book').on('click', function() {
        if (parseInt($('.room_book_sum label').text()) === 0) {
            $('.room_book_error').show();
            $('.rhd_room_error').show();
            $('.rhd_room_error').fadeOut(500, 0);
        } else {
            var hotel_id = $('.header_center_block').attr('value');
            var civic = $('.header_center_block').attr('civic');
            var date = $('.change_date');

            var rooms_id = '';
            var rooms_count = '';
            var session = '';
            var price = '';
            $('.rhd_room').each(function(i, v) {
                if (parseInt($(v).find('div.rhd_price_choice select').val()) > 0) {
                    rooms_id = rooms_id + $(v).attr('value') + ';';
                    session = session + $(v).attr('key') + ';';
                    price = price + $(v).attr('price') + ';';
                    rooms_count = rooms_count + $(v).find('div.rhd_price_choice select').val() + ';';
                }
            });

            window.location.assign(urlBooking + '/hotel/' + hotel_id + 
                    '/session/' + session + 
                    '/id/' + rooms_id + '/date/' + $(date).attr('dfrom') + 
                    '/night/' + createNight($(date).attr('dfrom'), $(date).attr('dto')) + '/counts/' + rooms_count + 
                    '/rooms/' + $('.room_button_book').attr('value') + '/price/' + price);
        }
    });

    $('.rhd_price_choice select').change(function() {
        $('.room_book_error').hide();
        totalSum = 0;
        $('.rhd_price_choice select').each(function(i, v) {
            var main = $(v).parent().parent();
            var price = $(main).find('div.rhd_price_sum').attr('price');
            var count = $(v).val();
            totalSum = totalSum + (parseFloat(count) * parseFloat(price));
        });

        $('.room_book_sum label').text(totalSum);
    });
});

function changeImage(direction) {
    var imagesBlock = [];
    var size = 0;
    var current;

    $('.main_images img').each(function(i, v) {
        imagesBlock.push($(v));
        size++;
        if ($(v).attr('selected') === 'selected') {
            current = $(v).attr('value');
        }
    });

    $(imagesBlock).each(function(i, v) {
        if ($(v).attr('value') === current) {
            $(v).removeAttr('selected');
            $(v).hide();
        }

        if (parseInt($(v).attr('value')) === getNextImage(direction, current, size)) {
            $(v).attr({'selected': 'selected'});
            $(v).show();
        }
    });
}

function getNextImage(direction, current, size) {
    if (parseInt(current) === 0 && direction === 'left') {
        return (parseInt(size) - 1);
    } else if (parseInt(current) > 0 && direction === 'left') {
        return (parseInt(current) - 1);
    } else if (parseInt(current) === (parseInt(size) - 1) && direction === 'right') {
        return 0;
    } else if (parseInt(current) < (parseInt(size) - 1) && direction === 'right') {
        return (parseInt(current) + 1);
    }
}

function createNight(from, to) {
    var f = from.split('-');
    var t = to.split('-');
    var trueF = f[1] + ',' + f[2] + ',' + f[1];
    var trueT = t[1] + ',' + t[2] + ',' + t[1];
    var night = new Date(trueT).getTime() - new Date(trueF).getTime();
    return (night / (1000 * 60 * 60 * 24));
}

function createGoogleMap() {
    var position = [32.104813299999996, 35.20501780000001];
    var location = new google.maps.LatLng(position[0], position[1]);

    var mapOptions = {
        center: location,
        zoom: 9,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("hotel_map"), mapOptions);
}


